/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import model.Contact;

/**
 *
 * @author Sacha MASSON
 */
public class FormAdd extends JFrame implements ActionListener {

    public JLabel l_lname;
    public JLabel l_fname;
    public JLabel l_address;
    public JLabel l_personalPhone;
    public JLabel l_proPhone;
    public JLabel l_email;
    public JLabel l_homePage;
    public JLabel l_other;
    public JLabel l_groupList;
    public JTextField name;
    public JTextField firstName;
    public JTextField homeAddress;
    public JTextField personalPhone;
    public JTextField proPhone;
    public JTextField email;
    public JTextField homePage;
    public JTextField other;
    public JFrame panel;
    public JComboBox group;
    public JButton button;
    public JButton browse;
    JFileChooser fc = new JFileChooser("img");
    int idprovider = 0;
    private ArrayList<Contact> contact;

    public FormAdd(ArrayList list) {

        super("Add a contact");
        this.setSize(600, 440);
        this.setDefaultCloseOperation(FormAdd.DISPOSE_ON_CLOSE);
        contact = list;
        panel = new JFrame();

        l_groupList = new JLabel("Group :");

        l_lname = new JLabel("Last Name :");
        name = new JTextField(15);

        // First name
        l_fname = new JLabel("First Name :");
        firstName = new JTextField(15);

        // Address
        l_address = new JLabel("Address :");
        homeAddress = new JTextField(15);

        // Personnal phone
        l_personalPhone = new JLabel("Personnal Phone :");
        personalPhone = new JTextField(15);

        // Professional phone
        l_proPhone = new JLabel("Professional Phone :");
        proPhone = new JTextField(15);

        // Mail
        l_email = new JLabel("Email :");
        email = new JTextField(15);

        // HomePage
        l_homePage = new JLabel("HomePage :");
        homePage = new JTextField(15);

        // Other
        l_other = new JLabel("Other :");
        other = new JTextField(15);

        //Button
        button = new JButton("Add");
        button.addActionListener(this);

        browse = new JButton("Browse");
        browse.addActionListener(this);

//Test with GridBagLayout
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        //Groups
        String[] groupInt = {"Family", "Friends", "Work"};
        group = new JComboBox(groupInt);
        this.add(group, c);

        //Name
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(name, c);

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_lname, c);

        //FName
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(firstName, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_fname, c);

        //Address
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(homeAddress, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_address, c);

        //Phone perso
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(personalPhone, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_personalPhone, c);

        //Pro phone
        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(proPhone, c);

        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_proPhone, c);

        //Email
        c.gridx = 1;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(email, c);

        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_email, c);

        //Home
        c.gridx = 1;
        c.gridy = 6;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(homePage, c);

        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_homePage, c);

        //Other
        c.gridx = 1;
        c.gridy = 7;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(other, c);

        c.gridx = 0;
        c.gridy = 7;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_other, c);

        //groups
        c.gridx = 1;
        c.gridy = 8;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(group, c);

        c.gridx = 0;
        c.gridy = 8;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(l_groupList, c);

        //button
        c.gridx = 2;
        c.gridy = 9;
        c.gridwidth = 1;
        c.gridheight = 1;

        this.add(button, c);

        this.setVisible(true);
        this.repaint();

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == button) {

            contact.add(new Contact(name.getText(), firstName.getText(), homeAddress.getText(), personalPhone.getText(), proPhone.getText(), homePage.getText(), other.getText(), email.getText(), "img/default.gif", group.getSelectedIndex()));
            JOptionPane.showMessageDialog(panel, "Contact successfully added");
            dispose();

        }

    }

}
